package br.net.woodstock.tickettoride.vaadin.web;

import java.io.Serializable;

import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

public class ViewManager implements Serializable {

	private static final long	serialVersionUID	= 5669391112759608940L;

	private Panel				window;

	private VerticalLayout		header;

	private VerticalLayout		body;

	public ViewManager(final Panel window) {
		super();
		this.window = window;
		this.header = new VerticalLayout();
		this.body = new VerticalLayout();

		VerticalLayout layout = new VerticalLayout();
		layout.setMargin(false);
		layout.setSpacing(false);

		layout.addComponent(this.header);
		layout.addComponent(this.body);

		this.window.addComponent(layout);
	}

	public void switchBody(final Layout view) {
		this.switchInternal(this.body, view);
	}

	public void switchHeader(final Layout view) {
		this.switchInternal(this.header, view);
	}

	private void switchInternal(final AbstractOrderedLayout target, final Layout view) {
		target.removeAllComponents();
		if (view != null) {
			target.addComponent(view);
			target.setComponentAlignment(view, Alignment.TOP_CENTER);
			target.setEnabled(true);
		} else {
			target.setEnabled(false);
		}
	}

}
